package com.moxiao.yfjh.module.order.controller.admin.brokeragerecord;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.member.controller.app.user.vo.AppTeamReqVo;
import com.moxiao.yfjh.module.member.dal.dataobject.user.MemberUserDO;
import com.moxiao.yfjh.module.member.service.user.MemberUserService;
import com.moxiao.yfjh.module.order.controller.admin.brokeragerecord.vo.*;
import com.moxiao.yfjh.module.order.controller.app.vo.AppWithdrawalReqVo;
import com.moxiao.yfjh.module.order.convert.brokeragerecord.BlindBrokerageRecordConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.brokeragerecord.BlindBrokerageRecordDO;
import com.moxiao.yfjh.module.order.tradeRefund.brokeragerecord.BlindBrokerageRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 相亲佣金记录")
@RestController
@RequestMapping("/order/brokerage-record")
@Validated
public class BlindBrokerageRecordController {

    @Resource
    private BlindBrokerageRecordService brokerageRecordService;
    @Resource
    private MemberUserService memberUserService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲佣金记录")
    public CommonResult<Integer> createBrokerageRecord(@Valid @RequestBody BlindBrokerageRecordCreateReqVO createReqVO) {
        return success(brokerageRecordService.createBrokerageRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲佣金记录")
    public CommonResult<Boolean> updateBrokerageRecord(@Valid @RequestBody BlindBrokerageRecordUpdateReqVO updateReqVO) {
        brokerageRecordService.updateBrokerageRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲佣金记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBrokerageRecord(@RequestParam("id") Integer id) {
        brokerageRecordService.deleteBrokerageRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲佣金记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BlindBrokerageRecordRespVO> getBrokerageRecord(@RequestParam("id") Integer id) {
        BlindBrokerageRecordDO brokerageRecord = brokerageRecordService.getBrokerageRecord(id);
        BlindBrokerageRecordRespVO vo = BlindBrokerageRecordConvert.INSTANCE.convert(brokerageRecord);
        return success(vo);
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲佣金记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<BlindBrokerageRecordRespVO>> getBrokerageRecordList(@RequestParam("ids") Collection<Integer> ids) {
        List<BlindBrokerageRecordDO> list = brokerageRecordService.getBrokerageRecordList(ids);
        return success(BlindBrokerageRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲佣金记录分页")
    public CommonResult<PageResult<BlindBrokerageRecordRespVO>> getBrokerageRecordPage(@Valid BlindBrokerageRecordPageReqVO pageVO) {
        PageResult<BlindBrokerageRecordRespVO> pageList = brokerageRecordService.getJoinRecordPage(pageVO);
        if (Objects.nonNull(pageList)) {
            List<BlindBrokerageRecordRespVO> list = pageList.getList();
            if (CollectionUtils.isNotEmpty(list)) {
                list.stream().forEach(item -> {
                    MemberUserDO memberUserDO = memberUserService.getUser(item.getUserId());
                    if (Objects.nonNull(memberUserDO)) {
                        item.setNickName(memberUserDO.getNickname());
                        item.setUserName(memberUserDO.getName());
                    } else {
                        item.setNickName("-");
                        item.setUserName("-");
                    }
                });
            }
        }
        return success(pageList);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲佣金记录 Excel")
    @OperateLog(type = EXPORT)
    public void exportBrokerageRecordExcel(@Valid BlindBrokerageRecordExportReqVO exportReqVO,
                                           HttpServletResponse response) throws IOException {
        List<BlindBrokerageRecordDO> list = brokerageRecordService.getBrokerageRecordList(exportReqVO);
        // 导出 Excel
        List<BlindBrokerageRecordExcelVO> datas = BlindBrokerageRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲佣金记录.xls", "数据", BlindBrokerageRecordExcelVO.class, datas);
    }

    /**
     * 我的团队列表
     */
    @GetMapping("/getTeam")
    @Operation(summary = "我的团队列表")
    @Parameter(name = "userId", description = "用户id", required = true)
    public CommonResult getTeam(@RequestParam("userId") Long userId) {
        List<AppTeamReqVo> list = memberUserService.getTeamList(userId);
        return success(list);
    }

    /**
     * 分销中心
     */
    @GetMapping("/getWithdrawal")
    @Operation(summary = "分销记录统计")
    @Parameter(name = "userId", description = "用户id", required = true)
    public CommonResult<AppWithdrawalReqVo> getBrokerageRecordPage(@RequestParam("userId") Long userId) {
        AppWithdrawalReqVo vo = brokerageRecordService.getWithdrawal(userId);
        return success(vo);
    }


}
